package ui.swing.table;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

public class Table__Container_ColumnWidth {
	public Table__Container_ColumnWidth() {
		JFrame f = new JFrame();
		f.setTitle("用了JScrollPane显示Header");
		f.setBounds(50,50,720,580);
		Object[][] playerInfo = {
				{ "阿呆", new Integer(66), new Integer(32), new Integer(98), new Boolean(false) },
				{ "阿瓜", new Integer(82), new Integer(69), new Integer(128), new Boolean(true) }
		};
		String[] Names = { "姓名", "语文", "数学", "总分", "及格" };
		JTable table = new JTable(playerInfo, Names);
		table.setPreferredScrollableViewportSize(new Dimension(550, 30));
		JScrollPane scrollPane = new JScrollPane(table);

		f.getContentPane().add(scrollPane, BorderLayout.CENTER);
		//f.pack();
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new Table__Container_ColumnWidth();
	}
}

class TableDemo1 {
	public TableDemo1() {
		JFrame f = new JFrame();
		f.setTitle("没有用JScrollPane，直接显示Header和Column");
		Object[][] playerInfo = {
				{ "阿呆", new Integer(66), new Integer(32), new Integer(98), new Boolean(false) },
				{ "阿瓜", new Integer(82), new Integer(69), new Integer(128), new Boolean(true) }
		};
		String[] Names = { "姓名", "语文", "数学", "总分", "及格" };
		JTable table = new JTable(playerInfo, Names);
		//JScrollPane scrollPane = new JScrollPane(table);
		//table.setPreferredScrollableViewportSize(new Dimension(550,30));

		f.getContentPane().add(table.getTableHeader(),BorderLayout.NORTH);
		f.getContentPane().add(table,BorderLayout.CENTER);

		f.pack();
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new TableDemo1();
	}
}

class TableDemo2 {
	public TableDemo2() {
		JFrame f = new JFrame();
		f.setTitle("调整列宽");
		Object[][] p={
				 {"阿呆",new Integer(66),new Integer(32),new Integer(98),new Boolean(false),new Boolean(false)},
                 {"阿呆",new Integer(82),new Integer(69),new Integer(128),new Boolean(true),new Boolean(false)},
		};	
		String[] n={"姓名","语文","数学","总分","及格","作弊"};

		JTable table = new JTable(p, n);
		//JScrollPane scrollPane = new JScrollPane(table);
		//table.setPreferredScrollableViewportSize(new Dimension(550,30));
		table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

		TableColumn column = null;
		//利用JTable中的getColumnModel()方法取得TableColumnModel对象;再利用TableColumnModel界面所定义的getColumn()方法取
        //TableColumn对象,利用此对象的setPreferredWidth()方法就可以控制字段的宽度.
		for (int i=0; i<6; i++) {
			column = table.getColumnModel().getColumn(i);
			if(i%2==0) {
				column.setPreferredWidth(150);
			}
			else
			{
				column.setPreferredWidth(50);
			}
		}
		f.getContentPane().add(table.getTableHeader(),BorderLayout.NORTH);
		f.getContentPane().add(table,BorderLayout.CENTER);

		f.pack();
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new TableDemo2();
	}
}